﻿using Crosstales.FB;
using System;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;

namespace Jinndev
{

    public static class Util
    {

        [StructLayout(LayoutKind.Sequential)]
        public struct STARTUPINFO
        {
            public uint cb;
            public string lpReserved;
            public string lpDesktop;
            public string lpTitle;
            public uint dwX;
            public uint dwY;
            public uint dwXSize;
            public uint dwYSize;
            public uint dwXCountChars;
            public uint dwYCountChars;
            public uint dwFillAttribute;
            public uint dwFlags;
            public ushort wShowWindow;
            public ushort cbReserved2;
            public IntPtr lpReserved2;
            public IntPtr hStdInput;
            public IntPtr hStdOutput;
            public IntPtr hStdError;
        }

        [DllImport("kernel32.dll")]
        public static extern void GetStartupInfo(out STARTUPINFO lpStartupInfo);

        /// <summary>
        /// 获得启动信息
        /// </summary>
        public static string GetStartPath()
        {
            GetStartupInfo(out STARTUPINFO info);
            return info.lpTitle;
        }

        public static void InitBrowsePasteFile(InputField inputField, string openTitle, string[] ext, string btnBrowse = "ButtonBrowse", string btnPaste = "ButtonPaste", string btnClear = "ButtonClear")
        {
            UIUtil.AddButtonClick(inputField, btnBrowse, () =>
            {
                OpenFilePanel(openTitle, inputField, ext);
            });
            UIUtil.AddButtonClick(inputField, btnPaste, () =>
            {
                string text = GUIUtility.systemCopyBuffer;
                if (!string.IsNullOrEmpty(text) && File.Exists(text))
                {
                    inputField.text = text;
                }
            });
            UIUtil.AddButtonClick(inputField, btnClear, () => inputField.text = "");
        }

        public static void InitBrowsePasteDirectory(InputField inputField, string openTitle, string btnBrowse = "ButtonBrowse", string btnPaste = "ButtonPaste", string btnClear = "ButtonClear")
        {
            UIUtil.AddButtonClick(inputField, btnBrowse, () =>
            {
                OpenDirectoryPanel(openTitle, inputField);
            });
            UIUtil.AddButtonClick(inputField, btnPaste, () =>
            {
                string text = GUIUtility.systemCopyBuffer;
                if (!string.IsNullOrEmpty(text) && Directory.Exists(text))
                {
                    inputField.text = text;
                }
            });
            UIUtil.AddButtonClick(inputField, btnClear, () => inputField.text = "");
        }

        /// <summary> 打开文件对话框 </summary>
        public static bool OpenFilePanel(string title, InputField inputField, params string[] filterExtensions)
        {
            string file = inputField.text;
            string dir = null;
            if (!string.IsNullOrEmpty(file) && File.Exists(file))
            {
                dir = Path.GetDirectoryName(file);
            }
            string path = OpenFilePanel(title, dir, filterExtensions);
            if (!string.IsNullOrEmpty(path))
            {
                inputField.text = path;
                return true;
            }
            //string[] files = StandaloneFileBrowser.OpenFilePanel(title, dir, new ExtensionFilter[] { new ExtensionFilter(null, filterExtensions) }, false);
            //if (files.Length > 0) {
            //    inputField.text = files[0];
            //    return true;
            //}
            return false;
        }

        /// <summary> 打开文件对话框 </summary>
        public static string OpenFilePanel(string title, string dir, params string[] filterExtensions)
        {
            return FileBrowser.Instance.OpenSingleFile(title, dir, string.Empty, filterExtensions);
        }

        /// <summary> 保存文件对话框 </summary>
        public static string SaveFilePanel(string title, string dir, string defaultName, params string[] filterExtensions)
        {
            return FileBrowser.Instance.SaveFile(title, dir, defaultName, filterExtensions);
        }

        /// <summary>
        /// 打开文件夹对话框
        /// </summary>
        public static bool OpenDirectoryPanel(string title, InputField inputField, string initDir = null)
        {
            return OpenFolderPanel(title, inputField, initDir);
        }

        /// <summary>
        /// 打开文件夹对话框
        /// </summary>
        public static bool OpenFolderPanel(string title, InputField inputField, string initDir = null)
        {
            string dir = initDir;
            if (dir == null)
            {
                dir = inputField.text;
                if (string.IsNullOrEmpty(dir) || !Directory.Exists(dir))
                {
                    dir = null;
                }
            }

            string path = FileBrowser.Instance.OpenSingleFolder(title, dir);
            if (!string.IsNullOrEmpty(path))
            {
                inputField.text = path;
                return true;
            }
            //string[] dirs = StandaloneFileBrowser.OpenFolderPanel(title, dir, false);
            //if (dirs.Length > 0) {
            //    inputField.text = dirs[0];
            //    return true;
            //}
            return false;
        }

        //public static void InitDirectoryInputField(InputField inputField, string settingKey, string defaultValue = null) {
        //    string dir = Setting.Get(settingKey, defaultValue);
        //    if (!string.IsNullOrEmpty(dir) && Directory.Exists(dir)) {
        //        inputField.text = dir;
        //    }
        //    inputField.onValueChanged.AddListener((value) => {
        //        if (!string.IsNullOrEmpty(value) && Directory.Exists(value)) {
        //            Setting.Set(settingKey, value);
        //        }
        //    });
        //}

        public static string FindProjectPath()
        {
            var dir = new DirectoryInfo(Directory.GetCurrentDirectory());
            dir = dir.Parent;
            while (dir != null)
            {
                string path = Path.Combine(dir.FullName, "client", "Crusher");
                if (Directory.Exists(Path.Combine(path, "assets")))
                {
                    return path;
                }
                dir = dir.Parent;
            }
            return null;
        }

        public static bool IsTransparent(Texture2D texture)
        {
            for (int x = 0; x < texture.width; x++)
            {
                for (int y = 0; y < texture.height; y++)
                {
                    if (texture.GetPixel(x, y).a < 1)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

    }


}
